//
//  CardCollectionViewCell.swift
//  koolcar
//
//  Created by kuaimaxiansheng on 2023/10/6.
//

import UIKit

class CardCollectionViewCell: UICollectionViewCell {
    var bgView: UIView!
    var carImageView: UIImageView!
    var titleLab: UILabel!
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = .white
        self.clipsToBounds = true
        
        self.bgView = UIView(frame: .zero)
        self.bgView.backgroundColor = UIColor(hex: 0xF7F7F7)
        self.bgView.layer.cornerRadius = 8
        self.bgView.clipsToBounds = true
        self.contentView.addSubview(self.bgView)
        
        self.carImageView = UIImageView(frame: .zero)
        self.carImageView.contentMode = .scaleAspectFit
        self.bgView.addSubview(self.carImageView)
        
        self.titleLab = UILabel(frame: .zero)
        self.titleLab.font = UIFont(name: Theme.PingFangSCRegular, size: 13)
        self.titleLab.textColor = Theme.titleLabColor
        self.titleLab.textAlignment = .center
        self.bgView.addSubview(self.titleLab)
        
        
        self.bgView.snp.makeConstraints { make in
            make.top.equalTo(0)
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.bottom.equalTo(0)
        }
        
        self.carImageView.snp.makeConstraints { make in
            make.top.equalTo(10)
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.height.equalTo(30)
        }
        
        self.titleLab.snp.makeConstraints { make in
            make.top.equalTo(self.carImageView.snp.bottom).offset(4)
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.height.equalTo(20)
        }
    }
    
    func setCardCollectionViewCell(model: CarModelDataList){
        self.titleLab.text = model.carCategory ?? ""
        if model.image ?? "" != "" {
            self.carImageView.image = UIImage(named: model.image ?? "")
        }else if let url = URL.init(string: model.carIconUrl ?? "") {
            self.carImageView.sd_setImage(with: url)
        }else{
            self.carImageView.image = UIImage.init(named: "icon_default_place_v")
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
